package com.baizhi.cmfzdemo.service.impl;

import com.baizhi.cmfzdemo.entity.CmfzArticle;
import com.baizhi.cmfzdemo.service.LuceneService;
import com.baizhi.cmfzdemo.util.LuceneUtil;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 54433 on 2019/4/15.
 */
@Service
public class LuceneServiceImpl implements LuceneService{
    @Autowired
    private LuceneService luceneService;

    @Override
    public int addIndexs(List<CmfzArticle> articles) {
        return 0;
    }

    @Override
    public boolean addIndexOne(CmfzArticle cmfzArticle) {
        return false;
    }

    @Override
    public boolean deleteAllIndex() throws IOException {
        //1。获取indexwriter对象
        try{
            IndexWriter indexWriter = LuceneUtil.getIndexWriter();
            indexWriter.deleteAll();
            LuceneUtil.commit(indexWriter);
            return true;
        }catch (Exception e){
            return false;

        }
    }

    @Override
    public boolean deleteOne(CmfzArticle cmfzArticle) {
        IndexWriter indexWriter = LuceneUtil.getIndexWriter();
        /**
         * 通过Term 删除 匹配id
         * id为TextField stringFi而来的是可以的
         * IntField
         */
        try {
            indexWriter.deleteDocuments(new Term("articleId", String.valueOf(cmfzArticle.getArticleId())));
            indexWriter.commit();
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    @Override
    public boolean updateOne(CmfzArticle cmfzArticle) {
        //1.获取IndexWriter对象
        IndexWriter indexWriter = LuceneUtil.getIndexWriter();
        //2.修改
        /**
         * 修改的时候 需要传入全部属性  不管要不要修改都得传
         * 数据的完整属性以数据库中的为准 索引库 缓存
         */
        try{
            Document document = new Document();
            int articleId = cmfzArticle.getArticleId();
            document.add(new IntField("articleId",articleId, Field.Store.YES));
            String articleName = cmfzArticle.getArticleName();
            document.add(new TextField("articleName",articleName,Field.Store.YES));
            String articleImage = cmfzArticle.getArticleImage();
            document.add(new TextField("articleImage",articleImage,Field.Store.YES));
            String articleContent = cmfzArticle.getArticleContent();
            document.add(new TextField("articleContent",articleContent,Field.Store.YES));
            return true;
        }catch (Exception e){
            return false;
        }
    }

    /**
     * 查询，根据用户输入信息做查询
     * @param keyword
     * @return
     */
    @Override
    public List<CmfzArticle> queryByKeyword(String keyword) throws IOException {
        //1.处理搜索词
        IndexSearcher indexSearcher = LuceneUtil.getIndexSearcher();
        Query query = LuceneUtil.getQuery(keyword);
        //3.读取索引文件
        FSDirectory directory = FSDirectory.open(new File("E://luceneTest"));
        DirectoryReader reader = DirectoryReader.open(directory);
        TopDocs topDocs = indexSearcher.search(query, 100);
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        List<CmfzArticle> cmfzArticles = new ArrayList<>();
        for (ScoreDoc scoreDoc : scoreDocs) {
//            5.获取与索引中的位置相信
            int docId = scoreDoc.doc;
            //6.通过document对象id获取对应的对象、document对象中封装了我们的数据
            Document document = indexSearcher.doc(docId);
            String articleId = document.get("articleId");
            String articleName = document.get("articleName");
            String articleImage = document.get("articleImage");
            String articleContent = document.get("articleContent");
            CmfzArticle cmfzArticle = new CmfzArticle(Integer.parseInt(articleId),articleName,articleImage,articleContent,null,null);
            cmfzArticles.add(cmfzArticle);
        }
        return cmfzArticles;
    }

    @Override
    public boolean resetIndexDB() {
        return false;
    }
}
